<template>
  <div class="app-container">
    <!-- <div class="filter-container">
      <el-button type="primary" @click="onAdd" icon="el-icon-plus">添加</el-button>
    </div> -->
    <el-table
      :data="list"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      lazy
      default-expand-all>
      <el-table-column
        prop="id"
        label="序号"
        width="100">
      </el-table-column>
      <el-table-column
        prop="account"
        label="发布者账号"
        width="140">
      </el-table-column>
      <el-table-column
        prop="nickname"
        label="发布者昵称"
        width="180">
      </el-table-column>
      <el-table-column
        prop="title"
        label="标题"
        width="200">
      </el-table-column>
      <el-table-column
        label="封面图"
        width="90">
        <template slot-scope="scope">
          <a target="_blank" :href="scope.row.information.cover">
            <img :src="scope.row.information.cover" alt="封面图" width="90" height="46">
          </a>
        </template>
      </el-table-column>
      <el-table-column
        prop="create_time"
        label="发布时间"
        min-width="170">
      </el-table-column>

      <el-table-column
        fixed="right"
        label="操作"
        width="220">
        <template slot-scope="scope">
          <el-button @click="lower(scope.row)" type="danger" size="small">下架</el-button>
          <el-button
            @click="gotop(scope.row)"
            :type="scope.row.is_top == 0 ? 'success' : 'warning'"
            size="small">{{scope.row.is_top == 0 ? '置顶' : '取置'}}</el-button>
          <el-button @click="look(scope.row)" type="primary" size="small">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />

    <el-dialog
      :title="content.title"
      :visible.sync="dialogVisible">
      <div>
        <div v-html="content.content"></div>
        <div v-if='content.info && content.info.image'>
          <a :href="item.url" target="_blank" v-for='item in content.info.image' :key='item.name'>
            <img :src="item.url" alt="图片">
          </a>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <!-- <el-button @click="dialogVisible = false">取 消</el-button> -->
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { list, istop, put } from '@/api/activity'
import Pagination from '@/components/Pagination'
export default {
  components: {
    Pagination
  },
  data() {
    return {
      content: {},
      list: [],
      total: 0,
      listLoading: false,
      dialogVisible: false,
      listQuery: {
        page: 1,
        limit: 10,
        sort: '-is_top,-create_time',
        where: [
          { field: 'cid_type', op: 'eq', value: 2 },
          { field: 'status', op: 'eq', value: 2 }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      list(this.listQuery).then(response => {
        if (response.status) {
          this.list = response.data.data
          this.total = response.data.total
          this.listLoading = false
        }
      })
    },

    // 置顶
    gotop(item) {
      const flag = !!item.is_top
      this.$confirm(`确定要${flag ? '取消置顶' : '置顶'}【${item.title}】吗？`, item.title, {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(() => {
        this.$store.commit('SHOW_LOADING')
        istop({
          type: Number(!flag),
          id: item.id
        }).then(res => {
          this.$message({
            type: 'success',
            message: '操作'
          })
          const cur = this.list.find(c => c.id === item.id)
          this.$set(cur, 'is_top', Number(!flag))
          // this.list.splice(this.list.findIndex(c => c.id === item.id), 1)
          this.$store.commit('HIDE_LOADING')
        })
      })
    },

    // 下架
    lower(item) {
      this.$confirm(`确定要下架【${item.title}】吗`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(({ value }) => {
        this.$store.commit('SHOW_LOADING')
        put(item.id, {
          status: 4
        }).then(res => {
          this.$message({
            type: 'success',
            message: '操作'
          })
          this.list.splice(this.list.findIndex(c => c.id === item.id), 1)
          this.$store.commit('HIDE_LOADING')
        })
      })
    },

    look(row) {
      this.content = row
      this.dialogVisible = true
    }
  }
}
</script>

<style>
img {max-width: 100%; vertical-align: middle;}
</style>
